﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BaseControls;
using Engine;

namespace ExtendedControls
{
    public class MenuItem<BeforeEventArgs, AfterEventArgs>
        where BeforeEventArgs : EventArgs,new()
        where AfterEventArgs : EventArgs,new()
    {
        public event EventHandler<BeforeEventArgs> BeforeCommandEvent;           
        public event EventHandler<AfterEventArgs> AfterCommandEvent;   
     
        protected virtual void OnAfterCommand(AfterEventArgs args)
        {
            if(AfterCommandEvent!=null)
                AfterCommandEvent(this,args);
        }

        protected virtual void OnBeforeCommand(BeforeEventArgs args)
        {
            if(BeforeCommandEvent != null)
                BeforeCommandEvent(this,args);
        }        
    }

    public class StartGame : MenuItem<EventArgs,EventArgs>, ICommand
    {
        public StartGame():base(){}        

        public void Do(FrameworkElement AssociatedObject)
        {
            OnBeforeCommand(new EventArgs());
            var findName  = (IEngineInterface)LogicalTreeHelper.FindName("FrameControl", AssociatedObject);
            if (findName != null)
            {
                findName.StartGame();
                Game game = findName.GetGame();                
                game.NewGame();
            }
            OnAfterCommand(new EventArgs());
        }
    }

    public class LoadGame : MenuItem<EventArgs, EventArgs>, ICommand
    {
        public LoadGame() : base() { }

        public void Do(FrameworkElement AssociatedObject)
        {
            OnBeforeCommand(new EventArgs());
            var findName = (IEngineInterface)LogicalTreeHelper.FindName("FrameControl", AssociatedObject);
            if (findName != null)
            {
                findName.LoadGame();
                Game game = findName.GetGame();
                game.LoadGame();
            }
            OnAfterCommand(new EventArgs());
        }
    }    

    public class LoadLanguageSelect : MenuItem<EventArgs, EventArgs>, ICommand
    {
        public LoadLanguageSelect() : base() { }

        public void Do(FrameworkElement AssociatedObject)
        {
            OnBeforeCommand(new EventArgs());
            var findName = (IEngineInterface)LogicalTreeHelper.FindName("FrameControl", AssociatedObject);
            if (findName != null)
                findName.LoadLanguageSelect();                            
            OnAfterCommand(new EventArgs());
        }
    }
  
    public class LoadCredits : MenuItem<EventArgs, EventArgs>, ICommand
    {
        public LoadCredits() : base() { }

        public void Do(FrameworkElement AssociatedObject)
        {
            OnBeforeCommand(new EventArgs());
            var findName = (IEngineInterface)LogicalTreeHelper.FindName("FrameControl", AssociatedObject);
            if (findName != null)
                findName.LoadCredits();                            
            OnAfterCommand(new EventArgs());
        }
    }    
}
